package com.spzx.test3;

import java.math.BigDecimal;
import java.util.concurrent.RecursiveTask;

public class Mutile extends RecursiveTask<BigDecimal> {
    Long start = 0L;
    Long end = 0L;
    BigDecimal result = new BigDecimal("1");

    public Mutile(Long start, Long end) {
        this.start = start;
        this.end = end;
    }

    @Override
    protected BigDecimal compute() {
        if ((end - start) < 10) {
            for (long i = start; i <= end; i++) {
                result = result.multiply(BigDecimal.valueOf(i));
            }
        } else {
            Long leftStart = start;
            Long leftEnd = (end + start) / 2;
            Long rightStart = leftEnd + 1;
            Long rightEnd = end;

            Mutile mutile1 = new Mutile(leftStart, leftEnd);
            Mutile mutile2 = new Mutile(rightStart, rightEnd);
            mutile1.fork();
            mutile2.fork();
            BigDecimal leftResult = mutile1.join();
            BigDecimal rightResult = mutile2.join();

            result = leftResult.multiply(rightResult) ;


        }
        return result;
    }
}
